package view;

import Dao.UserDao;
import Model.Obj.Member;
import Model.Obj.MemberCard;
import Model.OrcUtils.JDBCUtils;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;

public class MemberGUI extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField cardIDText;
    private JTextField membernameText;
    private JTextField textField_2;
    private JTextField memberText;
    private JTextField cardtypeText;
    private JTextField balanceText;
    private JTextField discountText;
    private DefaultTableModel dtm;
    private Set set =new HashSet<>();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MemberGUI frame = new MemberGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MemberGUI() {
        setTitle("\u4F1A\u5458\u8D44\u6599\u7BA1\u7406");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 646, 451);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();

        JLabel lblNewLabel = new JLabel("\u4F1A\u5458\u5361\u53F7");

        textField = new JTextField();
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("\u4F1A\u5458\u59D3\u540D");

        JButton btnNewButton = new JButton("\u67E5 \u8BE2");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButtonActionListener(e);
            }
        });

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("\u53EF\u8F93\u5165\u5361\u53F7\u6216\u4F1A\u5458\u59D3\u540D\u67E5\u8BE2");

        JButton btnNewButton_1 = new JButton("\u4FEE \u6539");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpateMemberActionPerformed(e);
            }
        });

        JLabel cardIDLabel = new JLabel("\u5361  \u53F7");

        cardIDText = new JTextField();
        cardIDText.setEnabled(false);
        cardIDText.setColumns(10);

        JLabel membernameLabel = new JLabel("\u59D3  \u540D");

        JLabel phoneLabel = new JLabel("\u7535  \u8BDD");

        membernameText = new JTextField();
        membernameText.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setColumns(10);

        JLabel memberIDLabel = new JLabel("\u8BC1\u4EF6\u53F7");

        JLabel balanceLabel = new JLabel("\u91D1  \u989D");

        JLabel discountLabel = new JLabel("\u6298  \u6263");

        JLabel cardtypeLabel = new JLabel("\u7C7B  \u578B");

        memberText = new JTextField();
        memberText.setColumns(10);

        cardtypeText = new JTextField();
        cardtypeText.setColumns(10);

        balanceText = new JTextField();
        balanceText.setEnabled(false);
        balanceText.setColumns(10);

        discountText = new JTextField();
        discountText.setColumns(10);

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.TRAILING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(21)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel_2)
                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                .addComponent(lblNewLabel)
                                                                .addGap(18)
                                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18)
                                                                .addComponent(lblNewLabel_1)
                                                                .addGap(18)
                                                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED, 200, Short.MAX_VALUE)
                                                                .addComponent(btnNewButton)))))
                                .addGap(20))
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(cardIDLabel)
                                                .addGap(18)
                                                .addComponent(cardIDText, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(membernameLabel)
                                                .addGap(18)
                                                .addComponent(membernameText))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(phoneLabel)
                                                .addGap(18)
                                                .addComponent(textField_2)))
                                .addGap(29)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(discountLabel)
                                                .addGap(18)
                                                .addComponent(discountText))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(balanceLabel)
                                                .addGap(18)
                                                .addComponent(balanceText))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(memberIDLabel)
                                                .addGap(18)
                                                .addComponent(memberText, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)))
                                .addGap(18)
                                .addComponent(cardtypeLabel)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(cardtypeText, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(46, Short.MAX_VALUE))
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap(281, Short.MAX_VALUE)
                                .addComponent(btnNewButton_1)
                                .addGap(276))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addComponent(lblNewLabel_2)
                                .addGap(10)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnNewButton))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(cardIDLabel)
                                        .addComponent(cardIDText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(memberIDLabel)
                                        .addComponent(memberText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(cardtypeLabel)
                                        .addComponent(cardtypeText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(30)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(membernameLabel)
                                        .addComponent(membernameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(balanceLabel)
                                        .addComponent(balanceText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(32)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(phoneLabel)
                                        .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(discountLabel)
                                        .addComponent(discountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                                .addComponent(btnNewButton_1)
                                .addGap(23))
        );
        table = new JTable();
        String[] column = {"卡号","姓名","电话","证件号","金额","折扣","类型"};
        String[][] data = new String[][]{};
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                MemberTableMousePressed(e);
            }
        });
        dtm = new DefaultTableModel(data,column){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(dtm);
        scrollPane.setViewportView(table);
        contentPane.setLayout(gl_contentPane);
    }

    private void btnNewButtonActionListener(ActionEvent e) {
        // TODO Auto-generated method stub
        List<MemberCard> list1 = null;
        List<MemberCard> list2 = null;
        if (set.contains(textField.getText()) || set.contains(textField_1.getText())){
            JOptionPane.showMessageDialog(null,"请勿重复查询");
            return;
        }
        if (textField.getText().isEmpty() && textField_1.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"请输入会员卡号或会员姓名");
            return;
        }
        String id = textField.getText();
        String name = textField_1.getText();
        if (!textField.getText().isEmpty()){
            list1 = UserDao.selectMemberCardID(id);
        }
        if (textField.getText().isEmpty() && !textField_1.getText().isEmpty()){
            list1 = UserDao.selectMemberCardName(name);
        }
        Vector v = new Vector<>();
        if (!textField.getText().isEmpty() && !textField_1.getText().isEmpty()){
            list1 = UserDao.selectMemberCardID(id);
            list2 = UserDao.selectMemberCardName(name);
            for (MemberCard memberCard : list1){
                if (list2.contains(memberCard.getCARDNO())){
                    v.add(memberCard.getCARDNO());
                    v.add(memberCard.getMEMBERNAME());
                    v.add(memberCard.getPHONE());
                    v.add(memberCard.getMEMBERID());
                    v.add(memberCard.getBALANCE());
                    v.add(memberCard.getDISCOUNT());
                    v.add(memberCard.getCARDTYPE());
                    dtm.addRow(v);
                } else {
                    JOptionPane.showMessageDialog(null,"会员卡号与会员姓名不符");
                    return;
                }
            }
        }
        for (MemberCard memberCard : list1){
            v.add(memberCard.getCARDNO());
            v.add(memberCard.getMEMBERNAME());
            v.add(memberCard.getPHONE());
            v.add(memberCard.getMEMBERID());
            v.add(memberCard.getBALANCE());
            v.add(memberCard.getDISCOUNT());
            v.add(memberCard.getCARDTYPE());
            dtm.addRow(v);
        }
    }

    protected void UpateMemberActionPerformed(ActionEvent e) {
        String cardno = cardIDText.getText();
        String name = membernameText.getText();
        String phone = textField_2.getText();
        String memberid = memberText.getText();
        String discount =discountText.getText();
        String cardtype = cardtypeText.getText();
        Integer i = Integer.valueOf(balanceText.getText());
        Integer j = Integer.valueOf(discountText.getText());
        if (cardtype.equals("储值卡")){
            j = 0;
        }
        if (cardtype.equals("折扣卡")){
            i = 0;
        }
        int count = UserDao.UpdateMemberCard(cardno,name,phone,memberid,j,cardtype,i);
        if (count >= 0){
            JOptionPane.showMessageDialog(null,"修改成功");
        } else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }

    protected void MemberTableMousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        int row = table.getSelectedRow();
        cardIDText.setText((String) table.getValueAt(row,0));
        membernameText.setText((String) table.getValueAt(row,1));
        textField_2.setText((String) table.getValueAt(row,2));
        memberText.setText((String) table.getValueAt(row,3));
        balanceText.setText(String.valueOf(table.getValueAt(row,4)));
        discountText.setText(String.valueOf(table.getValueAt(row,5)));
        cardtypeText.setText((String) table.getValueAt(row,6));
    }
}
